<template>
  <div class="container">
    <nav-bar></nav-bar>
    <div class="item-box">
      <div class="search-box">
        <div class="left-box">
          <pub-area
            :maxSelectLevel="3"
            @change="changeArea"
            customRightIcon
            label=""
            :isLink="false"
            valueIsLast
          ></pub-area>
        </div>
        <div class="right-box" v-show="useUserStore().orgInfo.level <= 2">
          <div class="text" @click="toDataView">区域分布情况</div>
        </div>
      </div>
      <div class="content-box">
        <div class="left-img">
          <img src="../../../assets/img/view/xjyljgzs.png" alt="" />
        </div>
        <div class="label">星级养老机构总数（个）</div>
        <div class="value">
          <count-to
            :startVal="0"
            :endVal="Number(state.info.total)"
            :decimals="0"
            :duration="2000"
          ></count-to>
        </div>
      </div>
      <ul class="card-box">
        <li @click="to(5)">
          <div class="title-box">五星级养老机构数</div>
          <div class="bottom-box">
            <div class="value">{{ state.info.rating5 }}</div>
            <div class="text">家</div>
          </div>
        </li>
        <li @click="to(4)">
          <div class="title-box">四星级养老机构数</div>
          <div class="bottom-box">
            <div class="value">{{ state.info.rating4 }}</div>
            <div class="text">家</div>
          </div>
        </li>
        <li @click="to(3)">
          <div class="title-box">三星级养老机构数</div>
          <div class="bottom-box">
            <div class="value">{{ state.info.rating3 }}</div>
            <div class="text">家</div>
          </div>
        </li>
        <li @click="to(2)">
          <div class="title-box">二星级养老机构数</div>
          <div class="bottom-box">
            <div class="value">{{ state.info.rating2 }}</div>
            <div class="text">家</div>
          </div>
        </li>
        <li @click="to(1)">
          <div class="title-box">一星级养老机构数</div>
          <div class="bottom-box">
            <div class="value">{{ state.info.rating1 }}</div>
            <div class="text">家</div>
          </div>
        </li>
      </ul>
    </div>
    <div class="item-box">
      <div class="title-box2">
        <div class="left-title">星级评定趋势图</div>
        <div class="right-title">近6个月数据</div>
      </div>
      <div class="chart-box2" ref="chart"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onBeforeMount, onMounted, inject } from 'vue'
import { getXJPD } from '@/api/xcc'
import router from '@/router/index'
import { useUserStore } from '@/pinia/modules/user'
import { useRouter } from 'vue-router'
import { CountTo } from 'vue3-count-to'
import * as echarts from 'echarts'

var option
const chart = ref()
const state = reactive({
  info: {},
  form: {
    areaCode: useUserStore().userLastArea
  }
})

const changeComponents = inject('changeComponents')

const changeArea = val => {
  state.form.areaCode = val
  getData()
}

const to = val => {
  changeComponents('orgList', val)
}

const toDataView = () => {
  changeComponents('dataView')
}

const initChart = data => {
  let chartDom = chart.value
  var myChart = echarts.init(chartDom)
  let month = []
  let rating1 = []
  let rating2 = []
  let rating3 = []
  let rating4 = []
  let rating5 = []
  data.trend.forEach(element => {
    month.push(element.code)
    rating1.push(element.rating1)
    rating2.push(element.rating2)
    rating3.push(element.rating3)
    rating4.push(element.rating4)
    rating5.push(element.rating5)
  })
  option = {
    color: ['#4375E4'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985'
        }
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '10%',
      top: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        data: month
      }
    ],
    yAxis: [
      {
        type: 'value',
        splitLine: {
          //   lineStyle: {
          //     // 设置背景横线
          //     color: 'red'
          //   }
        }
      }
    ],
    series: [
      {
        name: '一星级评定趋势',
        type: 'line',
        stack: 'Total',
        smooth: true,
        lineStyle: {
          width: 0
        },
        showSymbol: false,
        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#4375E4'
            },
            {
              offset: 1,
              color: 'rgba(67, 117, 228, 0)'
            }
          ])
        },
        emphasis: {
          focus: 'series'
        },
        data: rating1
      },
      {
        name: '二星级评定趋势',
        type: 'line',
        stack: 'Total',
        smooth: true,
        lineStyle: {
          width: 0
        },
        showSymbol: false,
        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#1FCAA8'
            },
            {
              offset: 1,
              color: 'rgba(31, 202, 168, 0)'
            }
          ])
        },
        emphasis: {
          focus: 'series'
        },
        data: rating2
      },
      {
        name: '三星级评定趋势',
        type: 'line',
        stack: 'Total',
        smooth: true,
        lineStyle: {
          width: 0
        },
        showSymbol: false,
        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#76C15C'
            },
            {
              offset: 1,
              color: 'rgba(118, 193, 92, 0)'
            }
          ])
        },
        emphasis: {
          focus: 'series'
        },
        data: rating3
      },
      {
        name: '四星级评定趋势',
        type: 'line',
        stack: 'Total',
        smooth: true,
        lineStyle: {
          width: 0
        },
        showSymbol: false,
        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#FEC02A'
            },
            {
              offset: 1,
              color: 'rgba(254, 192, 42, 0)'
            }
          ])
        },
        emphasis: {
          focus: 'series'
        },
        data: rating4
      },
      {
        name: '五星级评定趋势',
        type: 'line',
        stack: 'Total',
        smooth: true,
        lineStyle: {
          width: 0
        },
        showSymbol: false,
        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#EE6565'
            },
            {
              offset: 1,
              color: 'rgba(238, 101, 101, 0)'
            }
          ])
        },
        emphasis: {
          focus: 'series'
        },
        data: rating5
      }
    ]
  }

  option && myChart.setOption(option)
}

const getData = async () => {
  let res = await getXJPD(state.form)
  if (res.succeed) {
    state.info = res.data
    initChart(res.data)
  }
}

onMounted(() => {
  getData()
})
onBeforeMount(() => {})
</script>
<style lang="less" scoped>
.container {
  background-color: #f8f8f8;
  padding-top: 20px;
  .item-box {
    background-color: #fff;
    .search-box {
      display: flex;
      height: 90px;
      width: 100%;
      border-bottom: 1px solid #e6e6e6;
      justify-content: space-between;
      .left-box {
        height: 100%;
        :deep(.van-cell) {
          width: 220px;
          height: 100%;
          input {
            // max-width: 400px;
          }
        }
      }
      .right-box {
        height: 100%;
        margin-right: 40px;
        .text {
          line-height: 90px;
          font-size: 30px;
          font-weight: 400;
          color: #4375e4;
        }
      }
    }
    .content-box {
      padding: 30px 40px;
      display: flex;
      align-items: center;
      .left-img {
        width: 97px;
        height: 97px;
        img {
          width: 97px;
          height: 97px;
        }
      }
      .label {
        margin-left: 20px;
        font-size: 24px;
        font-family: PingFang SC;
        color: #333333;
      }
      .value {
        text-align: right;
        flex: 1;
        font-size: 36px;
        font-family: PingFang SC;
        color: #333333;
      }
    }
    .card-box {
      padding: 0px 25px 40px 25px;
      display: grid;
      grid-template-columns: repeat(3, 220px);
      grid-gap: 20px;
      li {
        padding: 20px 10px;

        border-radius: 13px;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        &:nth-child(1) {
          background: linear-gradient(214deg, #dbe7fd 0%, #ebf4fd 100%);
          box-shadow: 0px 2px 4px 0px rgba(12, 4, 49, 0.09);
        }
        &:nth-child(2) {
          background: linear-gradient(214deg, #ecfada 0%, #fafdf6 100%);
          box-shadow: 0px 2px 4px 0px rgba(12, 4, 49, 0.09);
        }
        &:nth-child(3) {
          background: linear-gradient(-84deg, #fcf1e0 0%, #fefbf6 100%);
          box-shadow: 0px 2px 4px 0px rgba(12, 4, 49, 0.09);
        }
        &:nth-child(4) {
          background: linear-gradient(214deg, #efe9fd 0%, #faf7fe 100%);
          box-shadow: 0px 2px 4px 0px rgba(12, 4, 49, 0.09);
        }
        &:nth-child(5) {
          background: linear-gradient(214deg, #e9fef7 0%, #fefefe 100%);
          box-shadow: 0px 2px 4px 0px rgba(12, 4, 49, 0.09);
        }
        .title-box {
          width: 100%;
          font-size: 24px;
          font-family: PingFang SC;
          color: #333333;
        }
        .bottom-box {
          width: 100%;
          height: 70px;
          line-height: 80px;
          padding: 0 5px;
          .value {
            display: inline-block;
            font-size: 48px;
            font-family: PingFang SC;
            font-weight: bold;
            color: #1b47f7;
          }
          .text {
            display: inline-block;
            font-size: 24px;
            font-family: PingFang SC;
            color: #333333;
            margin-left: 10px;
          }
        }
      }
    }
    .title-box2 {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
      padding: 30px 40px;
      border-bottom: 1px solid #e6e6e6;
      margin-bottom: 40px;
      .left-title {
        font-size: 30px;
        font-weight: bold;
        color: #000000;
      }
      .right-title {
        font-size: 24px;
        font-weight: 500;
        color: #999999;
      }
    }
    .chart-box2 {
      height: 450px;
    }
  }
}
</style>
